﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U.UI
{
    public partial class WebMaster_Default : System.Web.UI.Page
    {
        int LogoID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            LogoID = Convert.ToInt32(Request.QueryString["LogoID"]);
            if (Request.QueryString["Action"] == "Edit")
            {
                btSave.CommandArgument = "Edit";
                btSave.Text = " Update ";
                PhotoFile.Visible = false;
                if (!IsPostBack)
                {
                    Load_GetLinkByLogoID(LogoID);
                }
            }
        }

        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG")
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                    //duong dan tao save file Large
                    string strFilePathLarge = Request.PhysicalApplicationPath + "UserUpload/Link/" + strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, -1);
                }
                else
                {
                    PanelMsg.Visible = true;
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    strFileName = "";
                }
            }

            return strFileName;
        }


        protected void Load_GetLinkByLogoID(int LogoID)
        {
            webapp4U.BOL.Logo obj = webapp4U.BOL.Logo.GetLogoByID(LogoID);
            imgLogo.Visible = true;
            imgLogo.ImageUrl = "~/UserUpload/Link/" + obj.FileName;
            txtUrl.Text = obj.URL;
            txtOrder.Text = obj.DisplayOrder.ToString();
            dlPosition.SelectedValue = obj.Position.ToString();
            hImage.Value = obj.FileName;
        }


        protected void btSave_Click(object sender, EventArgs e)
        {
            // cap nhat du lieu vao database
            webapp4U.BOL.Logo obj = new webapp4U.BOL.Logo();
            obj.LogoID = LogoID;
            obj.URL = txtUrl.Text;
            obj.RegionDate = DateTime.Now;
            obj.ExpireDate = DateTime.MaxValue;

            if (txtRegionDate.Text != "")
                obj.RegionDate = Convert.ToDateTime(txtRegionDate.Text);
            if (txtExpireDate.Text != "")
                obj.ExpireDate = Convert.ToDateTime(txtExpireDate.Text);

            obj.Position = Convert.ToInt32(dlPosition.SelectedItem.Value);
            obj.DisplayOrder = Globals.ConvertToInt32(txtOrder.Text);
            obj.AddedDate = DateTime.Now;
            obj.IsActive = true;

            if (btSave.CommandArgument == "Edit")
            {
                obj.FileName = hImage.Value;
                webapp4U.BOL.Logo.Update(obj);
            }
            else
            {
                obj.FileName = FilePhoto_Upload();
                webapp4U.BOL.Logo.Insert(obj);
            }
            Response.Redirect(Globals.ApplicationPath + "WebMaster/Tools/Logo.aspx");
        }
    }
}